/**
 * @file test.c
 * @brief 硬件验证
 * @author wangh (wanghuan3037@fiberhome.com)
 * @version 1.0
 * @date 2020-10-26
 * @copyright Copyright (c) 2020  烽火通信
 ***************************************************************
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version  <th>Author  <th>Description
 * <tr><td>2020-10-26 <td>1.0      <td>wangh   <td>内容
 * </table>
 */ 
#ifdef PCU_TEST
#include "project_config.h"
#include "project_board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include <string.h>
#include <stdlib.h>
#include "bsp_gpio.h"
#include "bsp_uart_v2.h"
#include "easyflash.h"

#undef LOG_LEVEL_
#define LOG_LEVEL_ 	LOG_LVL_DEBUG
#include "bsp_log.h"

#include "drv_hc595_pcu.h"
#include "bsp_dac.h"
#include "bsp_adc.h"
#include "bsp_uart_v2.h"
#include "drv_misc.h"
#if DRV_BL0939_EN
#include "bl0939.h"
#endif
#if DRV_BL0906_EN
#include "bl0906.h"
#endif
#if DRV_LED_EN
#include "drv_led.h"
#endif
#if USE_BSP_RTC
#include "bsp_rtc.h"
#endif
#if WDT_ENABLE
#include "bsp_iwdg.h"
#endif


Gpio_t alarm_obj;       /* 告警灯对象 */
Gpio_t switch_obj;      /* 12V电源开关 */

Gpio_t MCU_DI1_obj;     /* 自定义DI1 */
Gpio_t MCU_DI2_obj;     /* 自定义DI2 */

pwrDect_t pwrDect_obj;  /* 智慧电源DI检测对象 */
pcuDac_t pcuDac_obj;    /* 智慧电源DAC调光对象 */
pcuAdc_t pcuAdc_obj;    /* 智慧电源ADC检测对象 */

#if DRV_BL0939_EN
Bl0939_t bl0939_obj;    /* 2主路电量计量对象 */
#endif
#if DRV_BL0906_EN
Bl0906_t bl0906_obj;    /* 6支路电量计量对象 */
#endif

#if DRV_LED_EN
LedDrv_t led_obj;    	/* 灯带对象 */
#endif

#if DRV_W5500_EN
#include "wiz.h"
#include "wiz_socket.h"
/**
 * @brief udp接收测试任务
 */
void udp_task(void *para)
{
    log_i("udp_task start... \r\n");

    int8_t socket = wiz_socket( Sn_MR_UDP, 6000, 0 );
    if (socket < 0)
    {
        log_e("wiz_socket failed(%d)!\r\n", socket);
        vTaskDelete( NULL );
        return;
    }
    log_d("UDP socket(%d) sucess!\r\n", socket);

    uint8_t remote_ip[4];
    uint16_t remote_port;
    uint8_t recv_buf[100];
    int32_t recv_len = 0;

    while (1)
    {
        memset(recv_buf, 0, 100);
        recv_len = wiz_recvfrom(socket, recv_buf, 100, remote_ip, &remote_port, portMAX_DELAY);
        if (recv_len > 0)
        {
            log_d("socket(%d) recvfrom(%d) %d.%d.%d.%d(%d): %s\r\n", socket, recv_len, remote_ip[0], remote_ip[1], remote_ip[2], remote_ip[3], remote_port, recv_buf);

            if (wiz_sendto(socket, recv_buf, recv_len, remote_ip, remote_port) < 0)
                log_e("wiz_sendto error\r\n");
        }

        vTaskDelay(1);
    }
}
#endif

/**
 * @brief RJ45基础RS485接收测试任务
 */
void uart1_task(void *para)
{
    log_i("uart1_task start... \r\n");

    while(1)
    {
        uint8_t buffer[257];
        memset(buffer, 0, 257);
        int32_t recv_len = UartRecv( UART_1, buffer, 256, portMAX_DELAY );
        if( recv_len > 0)
        {
            UartSend( UART_1, buffer, recv_len );
            log_d("RS485-1 recv(%d): %s\r\n", recv_len, buffer);
        }
        else
        {
            log_d("uart recv timeout\r\n");
        }

        vTaskDelay(1);
    }
}

/**
 * @brief RS485接收测试任务
 */
void uart4_task(void *para)
{
    log_i("uart4_task start... \r\n");

    while(1)
    {
        uint8_t buffer[257];
        memset(buffer, 0, 257);
        int32_t recv_len = UartRecv( UART_4, buffer, 256, portMAX_DELAY );
        if( recv_len > 0)
        {
            UartSend( UART_4, buffer, recv_len );
            log_d("RS485-2 recv(%d): %s\r\n", recv_len, buffer);
        }
        else
        {
            log_d("uart recv timeout\r\n");
        }

        vTaskDelay(1);
    }
}

/**
 * @brief 蓝牙串口接收测试任务
 */
void uart5_task(void *para)
{
    log_i("uart5_task start... \r\n");

    while(1)
    {
        uint8_t buffer[257];
        memset(buffer, 0, 257);
        int32_t recv_len = UartRecv( UART_5, buffer, 256, portMAX_DELAY );
        if( recv_len > 0)
        {
            log_d("BLE recv(%d):\r\n", recv_len);
            log_hexdump(buffer, recv_len);
        }
        else
        {
            log_v("uart recv timeout\r\n");
        }
        
        vTaskDelay(1);
    }
}

#if PCU_BL_STRESS_TEST
void bl1_task(void *para)
{
    Bl0939_Elect_t Elect;
    while (1)
    {
        Bl0939_GetElect( &bl0939_obj, &Elect, BL0939_ELECT_ALL );

        Bl0906_SetMn( &bl0906_obj, BL_M1_EN, true );
        Bl0906_Elect_t Elect;
        Bl0906_GetElect( &bl0906_obj, &Elect, BL0906_ELECT_V|BL0906_ELECT_PERIOD|BL0906_ELECT_I_ALL|BL0906_ELECT_P_ALL|BL0906_ELECT_L_6 );
        vTaskDelay(1);
    }
}

void bl2_task(void *para)
{
    Bl0939_Elect_t Elect;
    while (1)
    {
        Bl0939_GetElect( &bl0939_obj, &Elect, BL0939_ELECT_ALL );

        Bl0906_SetMn( &bl0906_obj, BL_M1_EN, true );
        Bl0906_Elect_t Elect;
        Bl0906_GetElect( &bl0906_obj, &Elect, BL0906_ELECT_V|BL0906_ELECT_PERIOD|BL0906_ELECT_I_ALL|BL0906_ELECT_P_ALL|BL0906_ELECT_L_6 );
        vTaskDelay(1);
    }
}

void bl3_task(void *para)
{
    Bl0939_Elect_t Elect;
    while (1)
    {
        Bl0939_GetElect( &bl0939_obj, &Elect, BL0939_ELECT_ALL );

        Bl0906_SetMn( &bl0906_obj, BL_M1_EN, true );
        Bl0906_Elect_t Elect;
        Bl0906_GetElect( &bl0906_obj, &Elect, BL0906_ELECT_V|BL0906_ELECT_PERIOD|BL0906_ELECT_I_ALL|BL0906_ELECT_P_ALL|BL0906_ELECT_L_6 );
        vTaskDelay(10);
    }
}

void bl4_task(void *para)
{
    Bl0939_Elect_t Elect;
    while (1)
    {
        Bl0939_GetElect( &bl0939_obj, &Elect, BL0939_ELECT_ALL );

        Bl0906_SetMn( &bl0906_obj, BL_M1_EN, true );
        Bl0906_Elect_t Elect;
        Bl0906_GetElect( &bl0906_obj, &Elect, BL0906_ELECT_V|BL0906_ELECT_PERIOD|BL0906_ELECT_I_ALL|BL0906_ELECT_P_ALL|BL0906_ELECT_L_6 );
        vTaskDelay(15);
    }
}
#endif

/**
 * @brief 电机任务
 */
void pcu_test_task(void *para)
{
    log_i("pcu_test_task start... \r\n");

#if USE_BSP_RTC
    RtcInit( ); /* RTC初始化 */
#endif

    /* 电源输出控制HC595初始化 */
    drv_pcuPwrCtr_init( );
    drv_pcuPwrCtr_Set( 0x3F, 0 );


    /* 电源输出状态获取初始化 */
    drv_pwrDect_init( &pwrDect_obj );


    GpioInit( &alarm_obj, ALARM, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 );   /* 告警灯引脚 */
    GpioInit( &switch_obj, SWITCH, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 ); /* switch引脚 */

    /* DI初始化 */
    GpioInit( &MCU_DI1_obj, MCU_DI1, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
    GpioInit( &MCU_DI2_obj, MCU_DI2, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );

    /* DAC初始化 */
    drv_pcuDac_init( &pcuDac_obj );
    drv_pcuDac_Set( &pcuDac_obj, 0, 0 );    /* 初始输出0 */

    /* ADC初始化 */
    drv_pcuAdc_init( &pcuAdc_obj );

    /* RS485初始化 */
#if (LOG_UART != 1)
    if( BSP_SUCCESS != UartInit( UART_1, UART1_TX_PIN, UART1_RX_PIN, 128 ))
        log_e("Uart1 Init fail !\r\n");
    else
    {
        UartConfig( UART_1, RX_TX, 9600, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
        xTaskCreate( uart1_task, "uart1_task", 256, NULL, 13, NULL);
    }
#endif
#if (LOG_UART != 4)
    if( BSP_SUCCESS != UartInit( UART_4, UART4_TX_PIN, UART4_RX_PIN, 128 ))
        log_e("Uart4 Init fail !\r\n");
    else
    {
        UartConfig( UART_4, RX_TX, 9600, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
        xTaskCreate( uart4_task, "uart4_task", 256, NULL, 13, NULL);
    }
#endif

    /* 蓝牙串口初始化 */
    if( BSP_SUCCESS != UartInit( UART_5, UART5_TX_PIN, UART5_RX_PIN, 128 ))
        log_e("Uart5 Init fail !\r\n");
    else
    {
        UartConfig( UART_5, RX_TX, 115200, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
        xTaskCreate( uart5_task, "uart5_task", 256, NULL, 13, NULL);
    }


    /* BL0939、BL0906初始化 */
#if DRV_BL0939_EN
    if( BSP_SUCCESS != Bl0939_Init( &bl0939_obj, UART_3, UART3_TX_PIN, UART3_RX_PIN, NC, 5 ) )
        log_e("Bl0939_Init fail !\r\n");
#endif

#if DRV_BL0906_EN
    if( BSP_SUCCESS != Bl0906_Init( &bl0906_obj, UART_2, UART2_TX_PIN, UART2_RX_PIN ) )
        log_e("Bl0906_Init fail !\r\n");
#endif

    /* 灯带 */
    // GpioWrite( &switch_obj, 1 );
#if DRV_LED_EN
    led_Init( &led_obj, WS2812, LED_DRIVE, 20 );
    RGB_t tcolor[1] = {0, 255, 0};
    led_set( &led_obj, &tcolor[0], 1, LED_KEEP_MODE, 0);
#endif

    /* W5500测试 */
#if DRV_W5500_EN
    wiz_init();
    xTaskCreate( udp_task, "udp_task", 256, NULL, 13, NULL);
#endif

#if YADA_TEST
    /* 雅达测试开机默认交直流输出全开 */
    drv_pcuPwrCtr_Set( PCU_PWR_ALL_SET, 1 );
#if DRV_BL0906_EN
    Bl0906_SetMn( &bl0906_obj, BL_M1_EN | BL_M2_EN | BL_M3_EN | BL_M4_EN | BL_M5_EN, false );
#endif
#endif

#if WDT_ENABLE
	iwdg_init( 5 );
#endif

#if PCU_BL_STRESS_TEST
    xTaskCreate( bl1_task, "bl1_task", 128, NULL, 10, NULL);
    xTaskCreate( bl2_task, "bl2_task", 128, NULL, 10, NULL);
    xTaskCreate( bl3_task, "bl3_task", 128, NULL, 11, NULL);
    xTaskCreate( bl4_task, "bl4_task", 128, NULL, 11, NULL);
#endif

    while(1)
    {
        /* BL0906 0939线程安全测试 */
        // Bl0939_Elect_t Elect;
        // Bl0939_GetElect( &bl0939_obj, &Elect, BL0939_ELECT_ALL );

        // Bl0906_SetMn( &bl0906_obj, BL_M1_EN, true );
        // Bl0906_Elect_t Elect;
        // Bl0906_GetElect( &bl0906_obj, &Elect, BL0906_ELECT_V|BL0906_ELECT_PERIOD|BL0906_ELECT_I_ALL|BL0906_ELECT_P_ALL|BL0906_ELECT_L_6 );
        // Bl0906_Energy_t Energy;
        // Bl0906_GetEnergy( &bl0906_obj, &Energy );

        /* drv_pcuAdc、drv_pcuDac、drv_pwrDect、drv_pcuPwrCtr 线程安全测试 */
        // drv_pcuAdc_Get( &pcuAdc_obj );
        // drv_pcuDac_Set( &pcuDac_obj, 2, 3 );
        // drv_pwrDect_Get( &pwrDect_obj );

        // drv_pcuPwrCtr_Set( 63, 1 );
        // uint8_t mask;
        // drv_pcuPwrCtr_Get( &mask );

        // RGB_t tcolor[1] = {0, 255, 0};
        // led_set( &led_obj, &tcolor[0], 1, LED_KEEP_MODE, 0);

#if WDT_ENABLE
        iwdg_feed();
#endif
        vTaskDelay(10);
    }
}

void application_start(void)
{
    xTaskCreate( pcu_test_task, "pcu_test", PCU_TEST_STK_SIZE, NULL, PCU_TEST_TASK_PRIO, NULL );
}

#if PCU_RELAY_TEST
#if DRV_BL0906_EN
static void relaytest_time_handler( TimerHandle_t xTimer )
{
    static uint8_t relay_state = 0;
    if (relay_state == 0)
    {
        relay_state = 1;
        Bl0906_SetMn( &bl0906_obj, BL_M1_EN | BL_M2_EN | BL_M3_EN | BL_M4_EN | BL_M5_EN, false );
    }
    else
    {
        relay_state = 0;
        Bl0906_SetMn( &bl0906_obj, BL_M1_EN | BL_M2_EN | BL_M3_EN | BL_M4_EN | BL_M5_EN, true );
    }
}
#endif
#endif

#if SHELL_TASK_EN
#include "nr_micro_shell.h"
#include "bsp_delay.h"
/**
 * @brief 电机测试指令
 */
void pcu_test_cmd(char argc, char *argv)
{
    if (argc >= 2)
    {
        if (!strcmp("help", &argv[argv[1]]))
        {
            shell_printf("pctr: power control by hc595 (pcu pctr set 63 0 )\r\n");
            shell_printf("pdect: power detect by DI (pcu pdect)\r\n");
            shell_printf("getdi: get the MCU DI (pcu getdi)\r\n");
            shell_printf("getadc: get the PCU ADC value (pcu getadc)\r\n");
            shell_printf("alarm: control the alarm led (motor alarm 0/1)\r\n");
            shell_printf("dac10: control the DAC1/2 (motor dac10 1 5)\r\n");
        }

        else if (!strcmp("pctr", &argv[argv[1]]))   /* 电源控制 pcu pctr set 3 1 */
        {
            if (!strcmp("set", &argv[argv[2]]))
            {
                if(argc < 5)
                {
                    shell_printf("pcu pctr set need more arguments!\r\n");
                    return;
                }
                drv_pcuPwrCtr_Set( (uint8_t)atoi(&argv[argv[3]]), (uint8_t)atoi(&argv[argv[4]]) );
            }
            else if (!strcmp("get", &argv[argv[2]]))
            {
                uint8_t pctr_state;
                drv_pcuPwrCtr_Get( &pctr_state );
                shell_printf("PwrCtr = 0x%02X\r\n", pctr_state);
            }
        }

        else if (!strcmp("pdect", &argv[argv[1]]))   /* 电源监测 pcu pdect */
        {
            drv_pwrDect_Get( &pwrDect_obj );
            shell_printf("pcu pwrDect_Get = %d %d %d %d %d %d\r\n", pwrDect_obj.v_BAT_CTRL, pwrDect_obj.v_Power_DECT1, pwrDect_obj.v_Power_DECT2, pwrDect_obj.v_Power_DECT3, pwrDect_obj.v_Power_DECT4, pwrDect_obj.v_Power_DECT5);
        }

        else if (!strcmp("getdi", &argv[argv[1]]))   /* DI测试 pcu getdi */
        {
            shell_printf("pcu getdi = %d %d\r\n", GpioRead( &MCU_DI1_obj ), GpioRead( &MCU_DI2_obj ));
        }

        else if (!strcmp("getadc", &argv[argv[1]]))   /* ADC测试 pcu getadc */
        {
            drv_pcuAdc_Get( &pcuAdc_obj );

            char str[200];
            snprintf( str, 200, "TC=%.3f  V_12V=%.3f  V_24V=%.3f  I_12V_1=%.3f  I_12V_2=%.3f  I_12V_3=%.3f  I_485_1=%.3f  I_485_2=%.3f  I_24V=%.3f", pcuAdc_obj.V_TC, pcuAdc_obj.V_12V, pcuAdc_obj.V_24V, pcuAdc_obj.I_12V_1, pcuAdc_obj.I_12V_2, pcuAdc_obj.I_12V_3, pcuAdc_obj.I_485_VCC1, pcuAdc_obj.I_485_VCC2, pcuAdc_obj.I_24V );

            shell_printf("%s\r\n", str);
        }

        else if (!strcmp("alarm", &argv[argv[1]]))  /* 告警灯输出 pcu alarm 1 */
        {
            if(argc < 3)
            {
                shell_printf("pcu alarm need more arguments!\r\n");
                return;
            }
            GpioWrite( &alarm_obj, (uint8_t)atoi(&argv[argv[2]]) );
        }

        else if (!strcmp("dac10", &argv[argv[1]]))  /* DAC测试 pcu dac10 1 0-10 */
        {
            if(argc < 4)
            {
                shell_printf("pcu dac10 need more arguments!\r\n");
                return;
            }
            float vt = atof(&argv[argv[3]]);
            if(vt > 10)
            {
                shell_printf("pcu dac set Vol need less than 10\r\n");
                return;
            }

            if (!strcmp("1", &argv[argv[2]]))
            {
                drv_pcuDac_Set( &pcuDac_obj, vt, 11 );
            }
            else if (!strcmp("2", &argv[argv[2]]))
            {
                drv_pcuDac_Set( &pcuDac_obj, 11, vt );
            }
        }

#if DRV_BL0939_EN
        else if (!strcmp("bl0939", &argv[argv[1]]))  /* 获取主路电量计量信息 pcu bl0939 */
        {
            Bl0939_Elect_t Elect;
            if (BSP_SUCCESS == Bl0939_GetElect( &bl0939_obj, &Elect, BL0939_ELECT_ALL ))
            {
            #if BL0939_ELECT_USE_FLOAT
                #if BL0939_V_RMS_EN
                shell_printf("Voltage_Value = %.3f\r\n", Elect.Voltage_Value );
                #endif
                #if BL0939_IA_RMS_EN
                shell_printf("Current_Value_A = %.3f\r\n", Elect.Current_Value_A );
                #endif
                #if BL0939_IB_RMS_EN
                shell_printf("Current_Value_B = %.3f\r\n", Elect.Current_Value_B );
                #endif
                #if BL0939_A_WATT_EN
                shell_printf("Power_Value_A = %.3f\r\n", Elect.Power_Value_A );
                #endif
                #if BL0939_B_WATT_EN
                shell_printf("Power_Value_B = %.3f\r\n", Elect.Power_Value_B );
                #endif
                #if BL0939_IA_FAST_RMS_EN
                shell_printf("LeakCur_Value_A = %.3f\r\n", Elect.LeakCur_Value_A );
                #endif
                #if BL0939_IB_FAST_RMS_EN
                shell_printf("LeakCur_Value_B = %.3f\r\n", Elect.LeakCur_Value_B );
                #endif
                #if BL0939_TPS1_EN
                shell_printf("TPS1 = %.3f\r\n", Elect.TPS1 );
                #endif
            #else
                #if BL0939_V_RMS_EN
                shell_printf("Voltage_Value = %d\r\n", Elect.Voltage_Value );
                #endif
                #if BL0939_IA_RMS_EN
                shell_printf("Current_Value_A = %d\r\n", Elect.Current_Value_A );
                #endif
                #if BL0939_IB_RMS_EN
                shell_printf("Current_Value_B = %d\r\n", Elect.Current_Value_B );
                #endif
                #if BL0939_A_WATT_EN
                shell_printf("Power_Value_A = %d\r\n", Elect.Power_Value_A );
                #endif
                #if BL0939_B_WATT_EN
                shell_printf("Power_Value_B = %d\r\n", Elect.Power_Value_B );
                #endif
                #if BL0939_IA_FAST_RMS_EN
                shell_printf("LeakCur_Value_A = %d\r\n", Elect.LeakCur_Value_A );
                #endif
                #if BL0939_IB_FAST_RMS_EN
                shell_printf("LeakCur_Value_B = %d\r\n", Elect.LeakCur_Value_B );
                #endif
                #if BL0939_TPS1_EN
                shell_printf("TPS1 = %d\r\n", Elect.TPS1 );
                #endif
            #endif
            }

            Bl0939_Energy_t Energy;
            if (BSP_SUCCESS == Bl0939_GetEnergy( &bl0939_obj, &Energy ))
            {
            #if BL0939_ENERGY_USE_FLOAT
                #if BL0939_CFA_EN
                shell_printf("Energy_A = %.5f\r\n", Energy.Energy_A );
                #endif
                #if BL0939_CFB_EN
                shell_printf("Energy_B = %.5f\r\n", Energy.Energy_B );
                #endif
            #else
                #if BL0939_CFA_EN
                shell_printf("Energy_A = %d\r\n", Energy.Energy_A );
                #endif
                #if BL0939_CFB_EN
                shell_printf("Energy_B = %d\r\n", Energy.Energy_B );
                #endif
            #endif
            }
        }
#endif

#if DRV_BL0906_EN
        else if (!strcmp("bl0906", &argv[argv[1]]))  /* 获取支路电量计量信息 pcu bl0906 show */
        {
            if(argc < 3)
            {
                shell_printf("pcu bl0906 need more arguments!\r\n");
                return;
            }
            if (!strcmp("k1", &argv[argv[2]]))      /* pcu bl0906 k1 1 */
            {
                if (!strcmp("0", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M1_EN, false );
                else if (!strcmp("1", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M1_EN, true );
                else
                    shell_printf("bl0906 cmd error!\r\n");
            }
            else if (!strcmp("k2", &argv[argv[2]])) /* pcu bl0906 k2 1 */
            {
                if (!strcmp("0", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M2_EN, false );
                else if (!strcmp("1", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M2_EN, true );
                else
                    shell_printf("bl0906 cmd error!\r\n");
            }
            else if (!strcmp("k3", &argv[argv[2]])) /* pcu bl0906 k3 1 */
            {
                if (!strcmp("0", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M3_EN, false );
                else if (!strcmp("1", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M3_EN, true );
                else
                    shell_printf("bl0906 cmd error!\r\n");
            }
            else if (!strcmp("k4", &argv[argv[2]])) /* pcu bl0906 k4 1 */
            {
                if (!strcmp("0", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M4_EN, false );
                else if (!strcmp("1", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M4_EN, true );
                else
                    shell_printf("bl0906 cmd error!\r\n");
            }
            else if (!strcmp("k5", &argv[argv[2]])) /* pcu bl0906 k5 1 */
            {
                if (!strcmp("0", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M5_EN, false );
                else if (!strcmp("1", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M5_EN, true );
                else
                    shell_printf("bl0906 cmd error!\r\n");
            }
            else if (!strcmp("k6", &argv[argv[2]])) /* pcu bl0906 k6 1 */
            {
                if (!strcmp("0", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M6_EN, false );
                else if (!strcmp("1", &argv[argv[3]]))
                    Bl0906_SetMn( &bl0906_obj, BL_M6_EN, true );
                else
                    shell_printf("bl0906 cmd error!\r\n");
            }
            else if (!strcmp("reset0", &argv[argv[2]]))
            {
                Bl0906_SoftRst( &bl0906_obj, 0 );   /* 系统复位 */
            }
            else if (!strcmp("reset1", &argv[argv[2]]))
            {
                Bl0906_SoftRst( &bl0906_obj, 1 );   /* 寄存器复位 */
            }
            else if (!strcmp("show", &argv[argv[2]]))
            {
                Bl0906_Elect_t Elect;
                if (BSP_SUCCESS == Bl0906_GetElect( &bl0906_obj, &Elect, BL0906_ELECT_V|BL0906_ELECT_PERIOD|BL0906_ELECT_I_ALL|BL0906_ELECT_P_ALL|BL0906_ELECT_L_6 ))
                {
                #if BL0906_ELECT_USE_FLOAT
                    #if BL0906_V_RMS_EN
                    shell_printf("RMS_V = %.3f\r\n", Elect.RMS_V );
                    #endif
                    #if BL0906_PERIOD_EN
                    shell_printf("Period = %.3f\r\n", Elect.Period );
                    #endif
                    #if BL0906_1_RMS_EN
                    shell_printf("Current_1 = %.3f\r\n", Elect.RMS_1 );
                    #endif
                    #if BL0906_2_RMS_EN
                    shell_printf("Current_2 = %.3f\r\n", Elect.RMS_2 );
                    #endif
                    #if BL0906_3_RMS_EN
                    shell_printf("Current_3 = %.3f\r\n", Elect.RMS_3 );
                    #endif
                    #if BL0906_4_RMS_EN
                    shell_printf("Current_4 = %.3f\r\n", Elect.RMS_4 );
                    #endif
                    #if BL0906_5_RMS_EN
                    shell_printf("Current_5 = %.3f\r\n", Elect.RMS_5 );
                    #endif
                    #if BL0906_WATT1_EN
                    shell_printf("Power_1 = %.3f\r\n", Elect.WATT_1 );
                    #endif
                    #if BL0906_WATT2_EN
                    shell_printf("Power_2 = %.3f\r\n", Elect.WATT_2 );
                    #endif
                    #if BL0906_WATT3_EN
                    shell_printf("Power_3 = %.3f\r\n", Elect.WATT_3 );
                    #endif
                    #if BL0906_WATT4_EN
                    shell_printf("Power_4 = %.3f\r\n", Elect.WATT_4 );
                    #endif
                    #if BL0906_WATT5_EN
                    shell_printf("Power_5 = %.3f\r\n", Elect.WATT_5 );
                    #endif
                    #if BL0906_6_FAST_RMS_EN
                    shell_printf("FAST_RMS_6 = %.3f\r\n", Elect.FAST_RMS_6 );
                    #endif
                #else
                    #if BL0906_V_RMS_EN
                    shell_printf("RMS_V = %d\r\n", Elect.RMS_V );
                    #endif
                    #if BL0906_PERIOD_EN
                    shell_printf("Period = %d\r\n", Elect.Period );
                    #endif
                    #if BL0906_1_RMS_EN
                    shell_printf("Current_1 = %d\r\n", Elect.RMS_1 );
                    #endif
                    #if BL0906_2_RMS_EN
                    shell_printf("Current_2 = %d\r\n", Elect.RMS_2 );
                    #endif
                    #if BL0906_3_RMS_EN
                    shell_printf("Current_3 = %d\r\n", Elect.RMS_3 );
                    #endif
                    #if BL0906_4_RMS_EN
                    shell_printf("Current_4 = %d\r\n", Elect.RMS_4 );
                    #endif
                    #if BL0906_5_RMS_EN
                    shell_printf("Current_5 = %d\r\n", Elect.RMS_5 );
                    #endif
                    #if BL0906_WATT1_EN
                    shell_printf("Power_1 = %d\r\n", Elect.WATT_1 );
                    #endif
                    #if BL0906_WATT2_EN
                    shell_printf("Power_2 = %d\r\n", Elect.WATT_2 );
                    #endif
                    #if BL0906_WATT3_EN
                    shell_printf("Power_3 = %d\r\n", Elect.WATT_3 );
                    #endif
                    #if BL0906_WATT4_EN
                    shell_printf("Power_4 = %d\r\n", Elect.WATT_4 );
                    #endif
                    #if BL0906_WATT5_EN
                    shell_printf("Power_5 = %d\r\n", Elect.WATT_5 );
                    #endif
                    #if BL0906_6_FAST_RMS_EN
                    shell_printf("FAST_RMS_6 = %d\r\n", Elect.FAST_RMS_6 );
                    #endif
                #endif
                }

                Bl0906_Energy_t Energy;
                if (BSP_SUCCESS == Bl0906_GetEnergy( &bl0906_obj, &Energy ))
                {
                #if BL0906_ENERGY_USE_FLOAT
                    #if BL0906_CF1_EN
                    shell_printf("Energy_1 = %.5f\r\n", Energy.Energy_1 );
                    #endif
                    #if BL0906_CF2_EN
                    shell_printf("Energy_2 = %.5f\r\n", Energy.Energy_2 );
                    #endif
                    #if BL0906_CF3_EN
                    shell_printf("Energy_3 = %.5f\r\n", Energy.Energy_3 );
                    #endif
                    #if BL0906_CF4_EN
                    shell_printf("Energy_4 = %.5f\r\n", Energy.Energy_4 );
                    #endif
                    #if BL0906_CF5_EN
                    shell_printf("Energy_5 = %.5f\r\n", Energy.Energy_5 );
                    #endif
                    #if BL0906_CF6_EN
                    shell_printf("Energy_6 = %.5f\r\n", Energy.Energy_6 );
                    #endif
                    #if BL0906_CF_EN
                    shell_printf("Energy_Sum = %.5f\r\n", Energy.Energy_Sum );
                    #endif
                #else
                    #if BL0906_CF1_EN
                    shell_printf("Energy_1 = %d\r\n", Energy.Energy_1 );
                    #endif
                    #if BL0906_CF2_EN
                    shell_printf("Energy_2 = %d\r\n", Energy.Energy_2 );
                    #endif
                    #if BL0906_CF3_EN
                    shell_printf("Energy_3 = %d\r\n", Energy.Energy_3 );
                    #endif
                    #if BL0906_CF4_EN
                    shell_printf("Energy_4 = %d\r\n", Energy.Energy_4 );
                    #endif
                    #if BL0906_CF5_EN
                    shell_printf("Energy_5 = %d\r\n", Energy.Energy_5 );
                    #endif
                    #if BL0906_CF6_EN
                    shell_printf("Energy_6 = %d\r\n", Energy.Energy_6 );
                    #endif
                    #if BL0906_CF_EN
                    shell_printf("Energy_Sum = %d\r\n", Energy.Energy_Sum );
                    #endif
                #endif
                }
            }
        }
#endif

#if DRV_W5500_EN
        else if (!strcmp("udp", &argv[argv[1]]))  /* udp发送测试 pcu udp 10 99 20 93 8080 hello */
        {
            if(argc < 8)
            {
                shell_printf("pcu udp need more arguments!\r\n");
                return;
            }

            uint8_t remote_ip[4] = {10,99,20,93};
            uint16_t remote_port = 8080;

            int8_t socket = wiz_socket( Sn_MR_UDP, 6001, 0 );
            if (socket < 0)
            {
                log_e("wiz_socket failed(%d)!\r\n", socket);
                return;
            }
            log_d("UDP socket(%d) sucess!\r\n", socket);

            remote_ip[0] = (uint8_t)atoi(&argv[argv[2]]);
            remote_ip[1] = (uint8_t)atoi(&argv[argv[3]]);
            remote_ip[2] = (uint8_t)atoi(&argv[argv[4]]);
            remote_ip[3] = (uint8_t)atoi(&argv[argv[5]]);
            remote_port = (uint16_t)atoi(&argv[argv[6]]);

            int32_t send_ret = wiz_sendto(socket, (uint8_t *)&argv[argv[7]], strlen(&argv[argv[7]]), remote_ip, remote_port);
            if (send_ret < 0)
            {
                log_e("wiz_sendto failed(%d)!\r\n", send_ret);
                return;
            }
            log_d("UDP socket(%d) send(%d) sucess!\r\n", socket, send_ret);

            int8_t ret = wiz_close(socket);
            if (ret != SOCK_OK)
            {
                log_e("wiz_close failed(%d)!\r\n", ret);
                return;
            }
            log_d("UDP socket(%d) close sucess!\r\n", socket);
        }

        else if (!strcmp("tcpc", &argv[argv[1]]))  /* TCP客户端发送测试 pcu tcpc 10 99 20 93 8080 hello */
        {
            if(argc < 8)
            {
                shell_printf("pcu tcpc need more arguments!\r\n");
                return;
            }

            uint8_t remote_ip[4] = {10,99,20,93};
            uint16_t remote_port = 8080;

            int8_t socket = wiz_socket( Sn_MR_TCP, 7000, Sn_MR_ND );
            if (socket < 0)
            {
                log_e("wiz_socket failed(%d)!\r\n", socket);
                return;
            }
            log_d("TCP socket(%d) sucess!\r\n", socket);

            remote_ip[0] = (uint8_t)atoi(&argv[argv[2]]);
            remote_ip[1] = (uint8_t)atoi(&argv[argv[3]]);
            remote_ip[2] = (uint8_t)atoi(&argv[argv[4]]);
            remote_ip[3] = (uint8_t)atoi(&argv[argv[5]]);
            remote_port = (uint16_t)atoi(&argv[argv[6]]);

            int8_t ret = wiz_connect(socket, remote_ip, remote_port);
            if (ret != SOCK_OK)
            {
                log_e("wiz_connect failed(%d)!\r\n", ret);
                return;
            }
            log_d("TCP socket(%d) connect sucess!\r\n", socket);

            int32_t send_ret = wiz_send( socket, (uint8_t *)&argv[argv[7]], strlen(&argv[argv[7]]) );
            if (send_ret < 0)
            {
                log_e("wiz_send failed(%d)!\r\n", send_ret);
                return;
            }
            log_d("TCP socket(%d) send(%d) sucess!\r\n", socket, send_ret);

            ret = wiz_disconnect(socket);
            if (ret != SOCK_OK)
            {
                log_e("wiz_disconnect failed(%d)!\r\n", ret);
                return;
            }
            log_d("TCP socket(%d) disconnect sucess!\r\n", socket);

            ret = wiz_close(socket);
            if (ret != SOCK_OK)
            {
                log_e("wiz_close failed(%d)!\r\n", ret);
                return;
            }
            log_d("TCP socket(%d) close sucess!\r\n", socket);
        }

#if WIZ_USING_DNS
        else if (!strcmp("dns", &argv[argv[1]]))  /* DNS测试 pcu dns www.baidu.com */
        {
            uint8_t dns_ip[4];
            if(wiz_gethostbyname(&argv[argv[2]], dns_ip) == 0)
                shell_printf("%d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]);
        }
#endif

        else if (!strcmp("netset", &argv[argv[1]]))  /* 设置网络参数 pcu netset ip 10 99 20 222 */
        {
            wiz_NetInfo netInfo;
            memset(&netInfo, 0, sizeof(wiz_NetInfo));
            if (!strcmp("ip", &argv[argv[2]]))
            {
                if (argc < 7)
                {
                    shell_printf("netset need more arguments!\r\n");
                    return;
                }
                netInfo.ip[0] = atoi(&argv[argv[3]]);
                netInfo.ip[1] = atoi(&argv[argv[4]]);
                netInfo.ip[2] = atoi(&argv[argv[5]]);
                netInfo.ip[3] = atoi(&argv[argv[6]]);
                wiz_set_network(&netInfo);
            }
            if (!strcmp("gw", &argv[argv[2]]))
            {
                if (argc < 7)
                {
                    shell_printf("netset need more arguments!\r\n");
                    return;
                }
                netInfo.gw[0] = atoi(&argv[argv[3]]);
                netInfo.gw[1] = atoi(&argv[argv[4]]);
                netInfo.gw[2] = atoi(&argv[argv[5]]);
                netInfo.gw[3] = atoi(&argv[argv[6]]);
                wiz_set_network(&netInfo);
            }
            if (!strcmp("sn", &argv[argv[2]]))
            {
                if (argc < 7)
                {
                    shell_printf("netset need more arguments!\r\n");
                    return;
                }
                netInfo.sn[0] = atoi(&argv[argv[3]]);
                netInfo.sn[1] = atoi(&argv[argv[4]]);
                netInfo.sn[2] = atoi(&argv[argv[5]]);
                netInfo.sn[3] = atoi(&argv[argv[6]]);
                wiz_set_network(&netInfo);
            }
            if (!strcmp("dns", &argv[argv[2]]))
            {
                if (argc < 7)
                {
                    shell_printf("netset need more arguments!\r\n");
                    return;
                }
                netInfo.dns[0] = atoi(&argv[argv[3]]);
                netInfo.dns[1] = atoi(&argv[argv[4]]);
                netInfo.dns[2] = atoi(&argv[argv[5]]);
                netInfo.dns[3] = atoi(&argv[argv[6]]);
                wiz_set_network(&netInfo);
            }
            if (!strcmp("dhcp", &argv[argv[2]]))    /* pcu netset dhcp 1 */
            {
                if (!strcmp("1", &argv[argv[3]]))
                {
                    netInfo.dhcp = NETINFO_DHCP;
                    wiz_set_network(&netInfo);
                }
                else if (!strcmp("0", &argv[argv[3]]))
                {
                    netInfo.dhcp = NETINFO_STATIC;
                    wiz_set_network(&netInfo);
                }
                else
                {
                    shell_printf("netset dhcp err arguments!\r\n");
                }
            }
        }
        
        else if (!strcmp("netget", &argv[argv[1]]))  /* 获取网络参数 pcu netget */
        {
            wiz_NetInfo netInfo;
            memset(&netInfo, 0, sizeof(wiz_NetInfo));
            wiz_get_network(&netInfo);
            shell_printf("MAC : %02X:%02X:%02X:%02X:%02X:%02X\r\n", netInfo.mac[0], netInfo.mac[1], netInfo.mac[2], netInfo.mac[3], netInfo.mac[4], netInfo.mac[5]);
            shell_printf("IP : %d.%d.%d.%d\r\n", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
            shell_printf("GW : %d.%d.%d.%d\r\n", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
            shell_printf("SN : %d.%d.%d.%d\r\n", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
            shell_printf("DNS : %d.%d.%d.%d\r\n", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
            if(netInfo.dhcp == NETINFO_DHCP)
                shell_printf("DHCP : NETINFO_DHCP\r\n");
            else
                shell_printf("DHCP : NETINFO_STATIC\r\n");
        }
#endif

#if (LOG_UART != 4)
        else if (!strcmp("rs485", &argv[argv[1]]))  /* RS485发送测试 pcu rs485 hello */
        {
            UartPrintf( UART_4, "%s\r\n", &argv[argv[2]] );
        }
#endif

#if (LOG_UART != 1)
        else if (!strcmp("rj485", &argv[argv[1]]))  /* RJ485发送测试 pcu rj485 hello */
        {
            UartPrintf( UART_1, "%s\r\n", &argv[argv[2]] );
        }
#endif

        /* 蓝牙拓展 */
        else if (!strcmp("ble", &argv[argv[1]]))    /* 蓝牙透传发送测试 pcu ble getangle */
        {
            uint8_t buffer2[100] = { 0xff, 0xe3,
                                    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
                                    0x11, 0x12, 0x13, 0x14,
                                    0x02, 0x23, 0x00, 0x00, /* 命令 */
                                    0x01, 0x00,
                                    0x00, 0x00, /* 净荷长度 */
                                    0x01, 0x02, 0x03, 0x04 };

            uint16_t buffer2_len = 31;
            if (!strcmp("send", &argv[argv[2]]))
            {
                buffer2[1] = 0xe3;
            }
            else if (!strcmp("getangle", &argv[argv[2]]))
            {
                buffer2[1] = 0xe4;
            }
            else if (!strcmp("oled", &argv[argv[2]]))   /* pcu ble oled hello */
            {
                buffer2[1] = 0xe4;
                buffer2[18] = 0x06;

                buffer2[30] = 1;
                buffer2[31] = 0;
                buffer2[32] = 32;
                buffer2[33] = 16;
                if(argc < 4)
                {
                    buffer2_len = 41;
                    buffer2[24] = 5+5;  /* 净荷长度 */
                    buffer2[34] = 5;    /* hello */
                    memcpy(&buffer2[35], "hello", 5);
                }
                else
                {
                    buffer2[34] = strlen(&argv[argv[3]]);
                    if (buffer2[34] > 64)
                        buffer2[34] = 63;
                    buffer2[24] = 5 + buffer2[34];
                    buffer2_len = 31 + buffer2[24];
                    memcpy(&buffer2[35], &argv[argv[3]], buffer2[34]);
                }
            }
            else
            {
                shell_printf("unsupport cmd\r\n");
                return;
            }

            uint8_t crc = 0;
            for (uint8_t i=0; i<buffer2_len-1; i++)
            {
                crc ^= buffer2[i];
            }
            buffer2[buffer2_len-1] = crc;

            UartSend( UART_5, buffer2, buffer2_len );
        }

        else if (!strcmp("delay", &argv[argv[1]]))  /* 延时精度测试 pcu delay 5530 100 */
        {
            uint32_t xStart = xTaskGetTickCount();
            vTaskDelay(100);
            shell_printf("100ms = %d\r\n", xTaskGetTickCount()-xStart);
            
            xStart = xTaskGetTickCount();
            uint16_t tms = atoi(&argv[argv[2]]);
            uint32_t ms = atoi(&argv[argv[3]]);
            xStart = xTaskGetTickCount();
            hal_delay_cal(ms, tms);
            shell_printf("%sms = %d\r\n", &argv[argv[3]], xTaskGetTickCount()-xStart);
        }

#if DRV_LED_EN
        else if (!strcmp("led", &argv[argv[1]]))  /* 控制带灯 pcu led r g b */
        {
            if(argc < 5)
                shell_printf("pcu led set need more arguments( pcu led r g b )!\r\n");

            RGB_t tcolor[1];
            tcolor[0].r = (uint8_t)atoi(&argv[argv[2]]);
            tcolor[0].g = (uint8_t)atoi(&argv[argv[3]]);
            tcolor[0].b = (uint8_t)atoi(&argv[argv[4]]);
            led_set( &led_obj, &tcolor[0], 1, LED_KEEP_MODE, 0 );
        }

        else if (!strcmp("ledwater", &argv[argv[1]]))  /* 控制带灯 pcu ledwater 1 */
        {
            if(argc < 3)
                shell_printf("pcu ledwater set need more arguments( pcu ledwater 1 )!\r\n");
            
            RGB_t tcolor[5];

            if (!strcmp("1", &argv[argv[2]]))
            {
                tcolor[0].r = 255;
                tcolor[0].g = 0;
                tcolor[0].b = 0;
                tcolor[1].r = 0;
                tcolor[1].g = 0;
                tcolor[1].b = 0;
                // tcolor[2].r = 0;
                // tcolor[2].g = 0;
                // tcolor[2].b = 0;
                // tcolor[3].r = 0;
                // tcolor[3].g = 0;
                // tcolor[3].b = 0;
                led_set( &led_obj, &tcolor[0], 2, LED_WATER_MODE, 1000);
                shell_printf("led water 1!\r\n");
            }
            if (!strcmp("2", &argv[argv[2]]))
            {
                tcolor[0].r = 0;
                tcolor[0].g = 0;
                tcolor[0].b = 255;
                tcolor[1].r = 0;
                tcolor[1].g = 0;
                tcolor[1].b = 0;
                tcolor[2].r = 0;
                tcolor[2].g = 0;
                tcolor[2].b = 0;
                // tcolor[3].r = 0;
                // tcolor[3].g = 0;
                // tcolor[3].b = 0;
                led_set( &led_obj, &tcolor[0], 3, LED_WATER_MODE, 100);
                shell_printf("led water 2!\r\n");
            }
            if (!strcmp("3", &argv[argv[2]]))
            {
                tcolor[0].r = 255;
                tcolor[0].g = 0;
                tcolor[0].b = 0;
                tcolor[1].r = 0;
                tcolor[1].g = 255;
                tcolor[1].b = 0;
                tcolor[2].r = 0;
                tcolor[2].g = 0;
                tcolor[2].b = 255;
                // tcolor[3].r = 0;
                // tcolor[3].g = 0;
                // tcolor[3].b = 0;
                led_set( &led_obj, &tcolor[0], 3, LED_WATER_MODE, 300);
                shell_printf("led water 3!\r\n");
            }
            if (!strcmp("4", &argv[argv[2]]))
            {
                tcolor[0].r = 255;
                tcolor[0].g = 255;
                tcolor[0].b = 0;
                tcolor[1].r = 0;
                tcolor[1].g = 255;
                tcolor[1].b = 0;
                tcolor[2].r = 0;
                tcolor[2].g = 0;
                tcolor[2].b = 255;
                tcolor[3].r = 100;
                tcolor[3].g = 55;
                tcolor[3].b = 99;
                tcolor[4].r = 33;
                tcolor[4].g = 44;
                tcolor[4].b = 55;
                led_set( &led_obj, &tcolor[0], 5, LED_WATER_MODE, 500);
                shell_printf("led water 4!\r\n");
            }
        }
#endif

#if USE_BSP_RTC
        else if (!strcmp("rtcset", &argv[argv[1]]))  /* 设置时间 pcu rtcset 2020 11 12 20 37 50 */
        {
            if (argc < 8)
            {
                shell_printf("rtcset need more arguments!\r\n");
                return;
            }
            rtc_time_t tm;
            tm.tm_year = atoi(&argv[argv[2]]);
            tm.tm_mon = atoi(&argv[argv[3]]);
            tm.tm_mday = atoi(&argv[argv[4]]);
            tm.tm_hour = atoi(&argv[argv[5]]);
            tm.tm_min = atoi(&argv[argv[6]]);
            tm.tm_sec = atoi(&argv[argv[7]]);
            RtcSet( &tm );
        }

        else if (!strcmp("rtcget", &argv[argv[1]]))  /* 获取时间 pcu rtcget */
        {
            rtc_time_t tm;
            RtcGet( &tm );
            #if RTC_CAL_WEEK
            shell_printf("current time: %d-%d-%d: %d-%d-%d  week(%d)\r\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_week);
            #else
            shell_printf("current time: %d-%d-%d: %d-%d-%d\r\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
            #endif
        }
#endif

#if LOG_USES_UART
        else if (!strcmp("debugset", &argv[argv[1]]))  /* 调试口波特率设置 pcu debugset 9600 */
        {
            if (!strcmp("4800", &argv[argv[2]]))    /* pcu debugset 9600 */
            {
                shell_printf("Debug 485 baud is set to 4800 !\r\n");
                UartConfig( DEBUG_UART, RX_TX, 4800, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
            }
            else if (!strcmp("9600", &argv[argv[2]]))    /* pcu debugset 9600 */
            {
                shell_printf("Debug 485 baud is set to 9600 !\r\n");
                UartConfig( DEBUG_UART, RX_TX, 9600, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
            }
            else if (!strcmp("19200", &argv[argv[2]]))
            {
                shell_printf("Debug 485 baud is set to 19200 !\r\n");
                UartConfig( DEBUG_UART, RX_TX, 19200, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
            }
            else if (!strcmp("38400", &argv[argv[2]]))
            {
                shell_printf("Debug 485 baud is set to 38400 !\r\n");
                UartConfig( DEBUG_UART, RX_TX, 38400, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
            }
            else if (!strcmp("57600", &argv[argv[2]]))
            {
                shell_printf("Debug 485 baud is set to 57600 !\r\n");
                UartConfig( DEBUG_UART, RX_TX, 57600, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
            }
            else if (!strcmp("115200", &argv[argv[2]]))
            {
                shell_printf("Debug 485 baud is set to 115200 !\r\n");
                UartConfig( DEBUG_UART, RX_TX, 115200, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
            }
            else
                shell_printf("pcu debugset cmd error or unsupport !\r\n");
        }
#endif

#if PCU_RELAY_TEST
#if DRV_BL0906_EN
        else if (!strcmp("relaytest", &argv[argv[1]]))  /* 调试口波特率设置 pcu relaytest on */
        {
            static uint8_t relaytest_timer_isCreate = 0;
            TimerHandle_t relaytest_timer;

            if (argc < 3)
            {
                shell_printf("relaytest need more arguments!\r\n");
                return;
            }

            if (!strcmp("on", &argv[argv[2]]))
            {
                if (relaytest_timer_isCreate == 0)
                {
                    relaytest_timer = xTimerCreate("relaytest", pdMS_TO_TICKS(1000), pdTRUE, NULL, relaytest_time_handler);
                    if(relaytest_timer == NULL)
                    {
                        log_e("relaytest timer create error\r\n");
                        return;
                    }
                    relaytest_timer_isCreate = 1;
                }
                if (argc == 3)
                    xTimerChangePeriod( relaytest_timer, pdMS_TO_TICKS( 30000 ), 0 );
                else
                {
                    if ((atoi(&argv[argv[3]]) < 5) || (atoi(&argv[argv[3]]) > 120))
                        xTimerChangePeriod( relaytest_timer, pdMS_TO_TICKS( 30000 ), 0 );
                    else
                        xTimerChangePeriod( relaytest_timer, pdMS_TO_TICKS( atoi(&argv[argv[3]]) * 1000 ), 0 );
                }
                xTimerStart( relaytest_timer, 0 );
            }
            else if ( (!strcmp("off", &argv[argv[2]])) && (relaytest_timer_isCreate == 1) )
            {
                xTimerStop( relaytest_timer, 0 );
            }
            else
                shell_printf("pcu relaytest cmd error or unsupport !\r\n");
        }
#endif
#endif

        else
        {
            shell_printf("unsupport cmd\r\n");
        }
    }
    else
    {
        shell_printf("pcu need more arguments!\r\n");
    }

}


NR_SHELL_CMD_EXPORT(pcu, pcu_test_cmd);

#endif	/* SHELL_TASK_EN */


#endif	/* PCU_TEST */

/*********** (C) COPYRIGHT 2020 FiberHome *****END OF FILE****/
